using Mathmall.Administration.Data;
using Mathmall.Administration.Models;
using Microsoft.EntityFrameworkCore;

namespace Mathmall.Administration.Mapper;

public class AttrAttrGroupRelationMapper : BaseMapper
{
    public AttrAttrGroupRelationMapper(AdministrationContext _context) : base(_context)
    {
    }

    /// <summary>
    /// 保存属性和属性分组的关联关系
    /// </summary>
    /// <param name="attrAttrgroupRelation"></param>
    /// <returns></returns>
    public async Task<int> addAttrAttrGroupRelation(AttrAttrgroupRelation attrAttrgroupRelation)
    {
        _context.AttrAttrgroupRelations.Add(attrAttrgroupRelation);
        return await _context.SaveChangesAsync();
    }

    /// <summary>
    /// 更新属性和分组的关联关系
    /// </summary>
    /// <param name="attrAttrgroupRelation"></param>
    /// <returns></returns>
    public async Task<int> UpdateAttrAttrGroupRelation(AttrAttrgroupRelation attrAttrgroupRelation)
    {
        return await _context.AttrAttrgroupRelations
            .Where(a => a.AttrID == attrAttrgroupRelation.AttrID)
            .ExecuteUpdateAsync(setter => setter.SetProperty(a => a.AttrGroupID, attrAttrgroupRelation.AttrGroupID));
    }

    /// <summary>
    /// 删除属性与分组的关联关系
    /// </summary>
    /// <param name="attrIds"></param>
    /// <returns></returns>
    public async Task<int> DeleteAttrAttrGroupRelation(List<int> attrIds)
    {
        return await _context.AttrAttrgroupRelations.Where(a => attrIds.Contains(a.AttrID)).ExecuteDeleteAsync();
    }
}